package com.nix.maze;

import com.nix.role.Dragon;
import com.nix.role.Role;
import com.nix.util.RandomUtil;

import java.util.ArrayList;
import java.util.Random;

/**
 * @author zhangpei
 * @Email zhangpe0312@qq.com
 * @day 2017/12/3 23:40
 * @Use 地图类
 */
public class Map {
    private int row = 20;
    private int lie = 20;
    private int dragonROW = -1;
    private int dragonLIE = -1;
    private int roleROW = -1;
    private int roleLIE = -1;

    /**
     * 食物
     */
    public final static int FOOD = 0;
    /**
     * 子弹
     */
    public final static int BULLET = 1;
    /**
     * 怪物
     */
    public final static int MONSTER = 2;

    /**
     * 空格子
     */
    public final static int isNull = 3;
    /**
     * 终点
     */
    public final static int isDRAGON = 4;
    /**
     * 角色
     */
    public final static int isROLE = 5;


    public final static String  LATTICE= "laatice";
    public final static String  DARGPM= "dargon";
    public final static String  ROLE= "role";
    //所有格子

    private ArrayList<Lattice> lattices = new ArrayList<>();

    public Map(int row, int lie) throws Exception {
        if (row < 0 || lie < 0) {
            throw new Exception("row 或者 lie 必须大于0");
        }
        this.row = row;
        this.lie = lie;
    }

    public void createMap() {
        String value = setValue(Map.isNull);
        //格子属性
        String classzz = LATTICE;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < lie; j++) {
                try {
                    lattices.add(createLattice(value, classzz, j, i,RandomUtil.random(4)));
                } catch (Exception e) {
                    j--;
                    e.printStackTrace();
                }
            }
        }
        //设置角色、终点
        try {
            setDragonIndex();
            for (int i = 0; i < lattices.size(); i++) {
                if (lattices.get(i).getxAddress() == roleLIE && lattices.get(i).getyAddress() == roleROW) {
                    lattices.get(i).setClasszz(ROLE);
                    lattices.get(i).setValue(setValue(isROLE));
                    Role.xAdress = roleLIE;
                    Role.yAdress = roleROW;
                    Role.index = i;
                    Role.direction = lattices.get(i).getDirection();
                }
                if (lattices.get(i).getxAddress() == dragonLIE && lattices.get(i).getyAddress() == dragonROW) {
                    Dragon.xAddress = dragonLIE;
                    Dragon.yAddress = dragonROW;
                    Dragon.index = i;
                    lattices.get(i).setClasszz(DARGPM);
                    lattices.get(i).setValue(setValue(isDRAGON));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void setLatticeValue(Map map,int number,int latticeValue) throws Exception {

        if(number>map.getMap().size()*0.5){
            System.out.println("你设置的数值太大，拒绝为你折这该道具！");
            return;
        }
        ArrayList<Lattice> lattices = map.getMap();
        int value[] = new int[number];
        int temp = -1;
        boolean falg;
        //产生number个不同的随机数且不能和角色和终点同位子
        for (int i = 0; i <number ; i++) {
            falg = false;
            temp = RandomUtil.random(lattices.size());
            //如果和角色或者终点位置相同，则
            if(temp==Role.index||temp==Dragon.index){
                i--;
                continue;
            }
            for (int j = 0; j <value.length ; j++) {
                if(temp==value[j]){
                    i--;
                    falg = true;
                    break;
                }
            }
            //如果没有相同的值，则存入数组中
            if(!falg){
                value[i] = temp;
            }
        }

        for (int i = 0; i <value.length ; i++) {
            lattices.get(value[i]).setClasszz(Map.LATTICE);
            lattices.get(value[i]).setValue(setValue(latticeValue));
        }
    }


    /**
     * 生成格子
     *
     * @param value
     * @param classzz
     * @param xAddress
     * @param yAddress
     * @return
     */
    private static Lattice createLattice(String value, String classzz,
                                         Integer xAddress, Integer yAddress,Integer direction) throws Exception {
        return new Lattice(value, classzz, xAddress, yAddress,direction);
    }

    /**
     * 设置终点坐标
     * @throws Exception
     */
    private void setDragonIndex() throws Exception {
        if (roleROW == -1 || roleLIE == -1) {
            setRoleIndex();
        }
        dragonROW = RandomUtil.random(row);
        dragonLIE = RandomUtil.random(lie);
        while (dragonROW == roleROW && dragonLIE == roleLIE) {
            dragonROW = RandomUtil.random(row);
            dragonLIE = RandomUtil.random(lie);
        }
    }

    /**
     * 设置角色坐标
     * @throws Exception
     */
    private void setRoleIndex() throws Exception {
        roleLIE = RandomUtil.random(lie);
        roleROW = RandomUtil.random(row);
    }

    /**
     * 随机选择格子里的属性
     *
     * @param index
     * @return
     */
    public static String setValue(int index) {
        switch (index) {
            case FOOD:
                return "food   ";
            case BULLET:
                return "bullet ";
            case MONSTER:
                return "monster";
            case isNull:
                return "null   ";
            case isDRAGON:
                return "DRAGON ";
            case isROLE:
                return "ROLE   ";
            default:
                return "null   ";
        }
    }

    /**
     * 打印地图
     * @param map 地图类
     */
    public static void printfMap(Map map) {
        System.out.println("setDragonIndex :  dragonLIE : " + Dragon.xAddress + "  dragonROW : " + Dragon.yAddress);
        System.out.println("setRoleIndex :  roleLIE : " + Role.xAdress + "  roleROW : " + Role.yAdress);
        ArrayList<Lattice> lattices = map.getMap();
        for (int i = 0; i < map.getLie(); i++) {
            if (i < 10) {
                System.out.print("|0" + i + "             ");
            } else {
                System.out.print("|"+i + "             ");
            }
        }
        System.out.println();
        for (int i = 0; i < lattices.size(); i++) {
            if (i % map.getLie() == 0 && i != 0) {
                System.out.println();
            }
            System.out.print(lattices.get(i).getValue() + " dir:" + lattices.get(i).getDirection());
        }
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getLie() {
        return lie;
    }

    public void setLie(int lie) {
        this.lie = lie;
    }

    public ArrayList<Lattice> getMap() {
        return lattices;
    }


    public static void main(String[] args) throws Exception {
        try {
            Map map = new Map(20, 20);
            map.createMap();
            System.out.println(map.getMap().size());
//            for (Lattice lattice : map.getMap()){
//                System.out.println(lattice.toString());
//            }
            printfMap(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
